import { createRouter, createWebHashHistory } from "vue-router";

const routes = [
  {
    path: "/",
    name: "Home",
    component: () =>
      import(/* webpackChunkName: "home" */ "../views/Home/index.vue"),
  },
  {
    path: "/login",
    name: "Login",
    component: () => import(/* webpackChunkName: "login" */ "../views/Login"),
  },
  {
    path: "/register",
    name: "Register",
    component: () =>
      import(/* webpackChunkName: "register" */ "../views/Register"),
  },
  {
    path: "/order",
    name: "Order",
    component: () => import(/* webpackChunkName: "order" */ "../views/Order"),
  },
  {
    path: "/pay",
    name: "Pay",
    component: () => import(/* webpackChunkName: "pay" */ "../views/Pay"),
  },
  {
    path: "/foodinfolist",
    name: "FoodInfoList",
    component: () =>
      import(/* webpackChunkName: "foodinfolist" */ "../views/FoodInfoList"),
  },
  {
    path: "/details",
    name: "Details",
    component: () =>
      import(/* webpackChunkName: "details" */ "../views/Details"),
  },
  {
    path: "/personal",
    name: "Personal",
    component: () =>
      import(/* webpackChunkName: "personal" */ "../views/Personal/index.vue"),
  },
  {
    path: "/orderDetail",
    name: "OrderDetail",
    component: () =>
      import(
        /* webpackChunkName: "OrderDetail" */ "../views/Personal/OrderDetail/index.vue"
      ),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});
router.beforeEach((to, from, next) => {
  // console.log("to", to, from);
  if (
    to.fullPath === "/login" ||
    to.fullPath === "/register" ||
    to.fullPath === "/"
  ) {
    next();
  } else if (JSON.parse(localStorage.getItem("user"))) {
    next();
  } else {
    next("/login");
  }
});
export default router;
